
#include "Config.h"
#include "reg.h"
#include "typedefs.h"
#include "Printf.h"
#include "I2C.h"
#include "CPU.h"
#include "SPI.h"
#include "NVMData.h"
#include "Global.h"
#include "KeyRemo.h"
#include "TouchScreen.h"
//#include "OSD\dv_OSD.h"
#include "OSD\app_OSD.h"
#include "OSD\User_handle.h"
#include "app_MSG.h"

BYTE LowADC = 0;  //Stone add 20100303 for key & touch

void TaskMSG(void)
{
	CMDST cmd;
	BOOL ret;

	ret = FALSE;

	//Send to GUIHandle
	if (!ret)
	{
		ret = CheckTSCCMD(&cmd);
	}
/*	
	if (!ret)
	{
 		ret = CheckKEYCMD(&cmd);
	}
	if (!ret)
	{
 		ret = CheckIRCMD(&cmd);
	}
*/	
 	if (ret)
	{
		//Printf("\r\n CheckCMD %bd %d %d %d %bd", cmd.cmdtype, cmd.Cmdbuf[0], cmd.Cmdbuf[1], cmd.Cmdbuf[2]&0x1fff, (U8)(cmd.Cmdbuf[2]>>13));
		SendMesssage((U8)cmd.cmdtype, 0, (U16)cmd.Cmdbuf[0], cmd.Cmdbuf[1], cmd.Cmdbuf[2]);
	}
	
	return;
}

void Init_MSGAPP(void)
{
	LowADC = 0;

	//----- Enable Touch
	InitTSC();
//	LoadParaTSC();

/*
	//----- Enable IR-Remo
	InitIR();
	//----- Enable Key
	InitKey();
*/

	return;
}

BOOL CheckIRCMD(CMDST* cmd)
{
	BYTE AutoKey=0, _RemoDataCode=0;

	cmd->cmdtype = CMDNULL;

	IntIR();
	if( 0 == IsRemoDataReady(&_RemoDataCode, &AutoKey)){
		return FALSE;
	}
	
	cmd->cmdtype = CMD_IR;

	cmd->Cmdbuf[1] = (U16)_RemoDataCode;
	if(_RemoDataCode == REMO_TWLL_EXIT){
		cmd->Cmdbuf[0] = KWUI_IR_EXIT;
	}else if(_RemoDataCode == REMO_TWLL_MENU){
		cmd->Cmdbuf[0] = KWUI_IR_MENU;
	}else if(_RemoDataCode == REMO_TWLL_VOLUP){
		cmd->Cmdbuf[0] = KWUI_IR_RIGHT;
	}else if(_RemoDataCode == REMO_TWLL_VOLDN){
		cmd->Cmdbuf[0] = KWUI_IR_LEFT;
	}else if(_RemoDataCode == REMO_TWLL_CHNUP){
		cmd->Cmdbuf[0] = KWUI_IR_UP;
	}else if(_RemoDataCode == REMO_TWLL_CHNDN){
		cmd->Cmdbuf[0] = KWUI_IR_DOWN;
	}else if(_RemoDataCode == REMO_TWLL_INPUT){
		cmd->Cmdbuf[0] = KWUI_IR_SEL;
	}else{
		cmd->Cmdbuf[0] = 0xff;
	}

	return TRUE;
}

BOOL CheckKEYCMD(CMDST* cmd)
{
	static BYTE RepeatKey = KEY_NONE;
	static DWORD KeyTic = 0;
	BYTE ikey;
	DWORD keytick;

	cmd->cmdtype = CMDNULL;
	
	if((LowADC == 0)||(LowADC == 2)){
		LowADC = IntKey();
	}
	ikey = ReadKey();
	if(ikey == 0){
		return FALSE;
	}

	//Repeat
	keytick = tic01;
	if ( ikey == RepeatKey) {
		if (((keytick >= KeyTic)&&(keytick-KeyTic < 100))
			||((keytick < KeyTic)&&(KeyTic-keytick > (120000-100)))){
			return FALSE;
		}
	}
	RepeatKey = ikey;
	KeyTic = keytick;
	
	cmd->cmdtype = CMD_KEY;
	cmd->Cmdbuf[1] = (U16)ikey;
	if(ikey == KEY_MENU){
		cmd->Cmdbuf[0] = KWUI_KEYPAD_MENU;
	}else if(ikey == KEY_LEFT){
		cmd->Cmdbuf[0] = KWUI_KEYPAD_LEFT;
	}else if(ikey == KEY_RIGHT){
		cmd->Cmdbuf[0] = KWUI_KEYPAD_RIGHT;
	}else if(ikey == KEY_UP){
		cmd->Cmdbuf[0] = KWUI_KEYPAD_UP;
	}else if(ikey == KEY_DOWN){
		cmd->Cmdbuf[0] = KWUI_KEYPAD_DOWN;
	}else if(ikey == KEY_INPUT){
		cmd->Cmdbuf[0] = KWUI_KEYPAD_SEL;
	}else{
		cmd->Cmdbuf[0] = 0xff;
	}

	return TRUE;
}

#define MOVE_X_THLD (5)	//eamon.fang
#define MOVE_Y_THLD (5)
#define MOVE_DX_THLD (160)
#define MOVE_DY_THLD (100)
#define HOLD_T_THLD (10)

U16 StartPtX = 0;
U16 StartPtY = 0;
U16 PrevVtX = 0;
U16 PrevVtY = 0;
BYTE AppState;
BYTE Direct = 0;

enum
{   
	APP_TSC_IDLE,
	APP_TSC_PRESS,
	APP_TSC_MOVE,
	APP_TSC_HOLD
};

BOOL CheckTSCCMD(CMDST *cmd)
{
	TSPOINT Point;
	BYTE State;

	S16 xgap;
	S16 ygap;

	cmd->cmdtype = CMDNULL;
	State        = TOUCH_IDLE;
	Point.x      = 0;
	Point.y      = 0;
	
	if ((LowADC == 0)||(LowADC == 1))
	{
		LowADC = IntTouch();
	}
	GetTouchInfo(&Point, &State);

	cmd->cmdtype = CMD_TOUCH;
	switch (State)
	{
    case TOUCH_IDLE:
		AppState = APP_TSC_IDLE;

		return FALSE;

    case TOUCH_PRESS:
		//OSD_BlockFill(Point.x, Point.y+480, 2, 2, 0xF800, 16);
		StartPtX       = Point.x;
		StartPtY       = Point.y;
		PrevVtX        = Point.x;
		PrevVtY        = Point.y;
		Direct         = 0;
		AppState       = APP_TSC_PRESS;
		cmd->Cmdbuf[0] = KWUI_TOUCH_DOWN;
		break;

    case TOUCH_HOLD:
		//OSD_BlockFill(Point.x, Point.y+480, 2, 2, 0xF800, 16);
		xgap = (S16)Point.x - (S16)PrevVtX;
		ygap = (S16)Point.y - (S16)PrevVtY;

		PrevVtX = Point.x;
		PrevVtY = Point.y;
		
		xgap = ((xgap>(-MOVE_X_THLD)) && (xgap<MOVE_X_THLD)) ? 0 : (xgap);
		ygap = ((ygap>(-MOVE_Y_THLD)) && (ygap<MOVE_Y_THLD)) ? 0 : (ygap);
		
		if ((xgap==0) && (ygap==0) && (AppState == APP_TSC_PRESS))
		{
			AppState = APP_TSC_HOLD;
			
			return FALSE;
		}
		else if ((xgap==0) && (ygap==0) && (AppState == APP_TSC_HOLD))
		{
			return FALSE;
		}
		else
		{
			AppState       = APP_TSC_MOVE;
			cmd->Cmdbuf[0] = KWUI_TOUCH_MOVING;
		}
		break;

    case TOUCH_POPUP:
		Point.x = StartPtX;
		Point.y = StartPtY;
		if (AppState == APP_TSC_MOVE)
		{
//			Point.x        = (U16)((S16)Point.x + PrevVtX);
//			Point.y        = (U16)((S16)Point.y + PrevVtY);
			AppState       = APP_TSC_IDLE;
			cmd->Cmdbuf[0] = KWUI_TOUCH_MOVE_END;
			//cmd->Cmdbuf[0] = KWUI_TOUCH_UP;
		}
		else
		{
			AppState       = APP_TSC_IDLE;
			cmd->Cmdbuf[0] = KWUI_TOUCH_UP;
		}
		break;

	default:
		AppState = APP_TSC_IDLE;

		return FALSE;
		break;
	}

	cmd->Cmdbuf[1] = Point.x;
	cmd->Cmdbuf[2] = Point.y;
	cmd->Cmdbuf[3] = StartPtX;
	cmd->Cmdbuf[4] = StartPtY;

	Point.x = (U16)(3*xgap - 5*ygap);
	Point.y = (U16)(3*xgap + 5*ygap);
//	Printf("\r\n %d|%d D %d|%d", Point.x, Point.y, PrevVtX, PrevVtY);
	if (((S16)Point.x) > 0)
	{
		if (((S16)Point.y) > 0)
		{
			Direct = APP_TSC_R;
		}
		else if (((S16)Point.y) < 0)
		{
			Direct = APP_TSC_U;
		}
		else
		{
			Direct = APP_TSC_R;
		}
	}
	else if (((S16)Point.x) < 0)
	{
		if (((S16)Point.y) > 0)
		{
			Direct = APP_TSC_D;
		}
		else if (((S16)Point.y) < 0)
		{
			Direct = APP_TSC_L;
		}
		else
		{
			Direct = APP_TSC_L;
		}
	}
	else
	{
		if (((S16)Point.y) > 0)
		{
			Direct = APP_TSC_R;
		}
		else if (((S16)Point.y) < 0)
		{
			Direct = APP_TSC_L;
		}
		else
		{
		}
	}
	
	cmd->Cmdbuf[2] |= (U16)Direct<<13;

	return TRUE;
}




